UNPKG

9.13 kBHTMLView Raw
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <title>JSDoc: Tutorial: Indexing and Query performance</title>
6
7 <script src="scripts/prettify/prettify.js"> </script>
8 <script src="scripts/prettify/lang-css.js"> </script>
9 <!--[if lt IE 9]>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11 <![endif]-->
12 <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13 <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14</head>
15
16<body>
17
18<div id="main">
19
20 <h1 class="page-title">Tutorial: Indexing and Query performance</h1>
21
22 <section>
23
24<header>
25
26
27 <h2>Indexing and Query performance</h2>
28</header>
29
30<article>
31 <p>Loki.js has always been a fast, in-memory database solution. In fact, recent benchmarks indicate that its primary get() operation is about <em>1.4 million operations</em> per second fast on a mid-range Core i5 running under node.js. The get() operation utilizes an auto generated '$loki' id column with its own auto generated binary index. If you wish to supply your own unique key, you can use add a single unique index to the collection to be used along with the collection.by() method. This method is every bit as fast as using the built in $loki id. So out of the gate if you intend to do single object lookups you get this performance.</p>
32<p>Example object lookup specifying your own unique index :</p>
33<pre class="prettyprint source lang-javascript"><code>var users = db.addCollection(&quot;users&quot;, {
34 unique: ['username']
35});
36
37// after inserting records you might retrieve your record using coll.by()
38var result = users.by(&quot;username&quot;, &quot;Heimdallr&quot;);</code></pre><p>Example lookup using autogenerated $loki column : </p>
39<pre class="prettyprint source lang-javascript"><code>var users = db.addCollection(&quot;users&quot;);
40var resultObj = users.insert({username:&quot;Heimdallr&quot;});
41
42// now that our object has been inserted, it will have a $loki property added onto it
43var heimdallr = users.get(resultObj.$loki);</code></pre><p>A more versatile way to query is to use collection.find() which accepts a mongo-style query object. If you do not index the column you are searching against, you can expect about 20k ops/sec under node.js (browser performance may vary but this serves as a good order of magnitude). For most purposes that is probably more performance than is needed, but you can now apply loki.js binary indexes on your object properties as well. Using the collection.ensureIndex(propertyName) method, you can create an index which can be used by various find() operations such as collection.find(). For our test benchmark, this increased performance to about <em>500k ops/sec</em>.</p>
44<p>These binary indices can match multiple results or ranges and you might apply your index similar to in this example : </p>
45<pre class="prettyprint source lang-javascript"><code>var coll = db.addCollection('users', {
46 indices: ['location']
47});
48
49// after inserting records you might use equality or range ops,
50// such as this implicit $eq op :
51var results = users.find({ location: 'Himinbjörg' });</code></pre><p>'Where' filters (javascript filter functions) should be used sparingly if performance is of concern. It is unable to utilize indexes, so performance will be no better than an unindexed find, and depending on the complexity of your filter function even less so. Unindex queries and where filters always require a full array scan but they can be useful if thousands of ops/sec are sufficient or if used later in a query chain or dynamic view filter pipeline with less penalty.</p>
52<p>The Resultset class introduced method chaining as an option for querying. You might use this method chaining to apply several find operations in succession or mix find(), where(), and sort() operations into a sequential chained pipe. For simplicity, an example of this might be (where users is a collection object) :</p>
53<pre class="prettyprint source lang-javascript"><code> users.chain().find(queryObj).where(queryFunc).simplesort('name').data();</code></pre><p>Examining this statement, if queryObj (a mongo-style query object) were { 'age': { '$gt': 30 } }, then that age column would be best to apply an index on, and that find() chain operation should come first in the chain. In chained operations, only the first chained operation can utilize the indexes for filtering. If it filtered out a sufficient number of records, the impact of the (where) query function will be less. The overhead of maintaining the filtered result set reduces performance by about 20% over collection.find, but they enable much more versatility. In our benchmarks this is still about <em>400k ops/sec</em>.</p>
54<p>Dynamic Views behave similarly to resultsets in that you want to utilize an index, your first filter must be applied using</p>
55<pre class="prettyprint source lang-javascript"><code> var userview = users.addDynamicView(&quot;over30&quot;);
56 userview.applyFind({'Age': {'$gte':30}});
57
58 // at any time later you can grab the latest view results
59 var results = userview.data();
60
61 // or branch the results for further filtering
62 results = userview.branchResultset().find({'Country': 'JP'}).data();</code></pre><p>That find filter should ideally refer to a field which you have applied an index to ('Age' in this case). Dynamic Views run their filters once however, so even non performant query pipelines are fast after they are set up. This is due to re-evaluation of those filters on single objects as they are inserted, updated, or deleted from the collection. Being single object evaluations there is no array scan penalty which occurs during the first evaluation. The overhead of dynamic views, which ride on top of the resultset, reduces performance of the first evaluation by about 40%, however subsequent queries are highly optimized (faster than collection.find). Even with that overhead, our benchmarks show roughly <em>300k ops/sec</em> performance on initial evaluation. Depending on update frequency, subsequent evaluations can scale up to over 1 million ops/sec.</p>
63<p>In loki.js, Dynamic Views have currently have two options, 'persistent' (default is false) and 'sortPriority' (default is 'passive'). </p>
64<p>The '<strong>persistent</strong>' option indicates that results will be kept in an internal array (in addition to normal resultset). This 'resultdata' array is filtered and sorted according to your specifications. This copying of results into the internal array occurs during the first data() evaluation or once filters or sorts are dirty (documents inserted, updated, removed from view). This options adds memory overhead, but possibly optimizes data() calls. </p>
65<p>The '<strong>sortPriority</strong>' option can be either 'passive' or 'active'. By default sorting occurs lazily ('passive') when data() is called and the sorts are flagged as dirty. If you wish the sorting cost to be 'up-front', you can specify 'active' sortPriority. With active sortPriority, once an insert/update/delete flags a sort as dirty, we will queue and throttle an async sort to run when the thread yields. So with lower update frequency, or isolated batched modifications, you can pay the performance cost up-front to ensure optimal data() retrieval speed later. If your data modifications are frequent and sporadic, an active sortPriority might waste computation sorting if no one ever reads the data.</p>
66</article>
67
68</section>
69
70</div>
71
72<nav>
73 <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Collection.html">Collection</a></li><li><a href="DynamicView.html">DynamicView</a></li><li><a href="Loki.html">Loki</a></li><li><a href="LokiEventEmitter.html">LokiEventEmitter</a></li><li><a href="LokiFsAdapter.html">LokiFsAdapter</a></li><li><a href="LokiFsStructuredAdapter.html">LokiFsStructuredAdapter</a></li><li><a href="LokiIndexedAdapter.html">LokiIndexedAdapter</a></li><li><a href="LokiLocalStorageAdapter.html">LokiLocalStorageAdapter</a></li><li><a href="LokiMemoryAdapter.html">LokiMemoryAdapter</a></li><li><a href="LokiPartitioningAdapter.html">LokiPartitioningAdapter</a></li><li><a href="Resultset.html">Resultset</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-Autoupdating Collections.html">Autoupdating Collections</a></li><li><a href="tutorial-Changes API.html">Changes API</a></li><li><a href="tutorial-Collection Transforms.html">Collection Transforms</a></li><li><a href="tutorial-Indexing and Query performance.html">Indexing and Query performance</a></li><li><a href="tutorial-Loki Angular.html">Loki Angular</a></li><li><a href="tutorial-Persistence Adapters.html">Persistence Adapters</a></li><li><a href="tutorial-Query Examples.html">Query Examples</a></li></ul>
74</nav>
75
76<br class="clear">
77
78<footer>
79 Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Sun Dec 18 2016 19:39:52 GMT-0500 (Eastern Standard Time)
80</footer>
81
82<script> prettyPrint(); </script>
83<script src="scripts/linenumber.js"> </script>
84</body>
85</html>
\No newline at end of file